package com.springsummer.information.green.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class ClassPathResource implements Resource {

    private String path;

    private ClassLoader classLoader;

    public ClassPathResource(String path) {
        this.path = path;

        ClassLoader classLoaderTemp = Thread.currentThread().getContextClassLoader();
        if (classLoaderTemp == null) {
            //// No thread context class loader -> use class loader of this class.
            //			cl = ClassUtils.class.getClassLoader();
            classLoaderTemp = ClassPathResource.class.getClassLoader();
        }
        this.classLoader = classLoaderTemp;

    }

    @Override
    public InputStream getInputStream() throws IOException {
        return classLoader.getResourceAsStream(this.path);
    }

    @Override
    public URL getURL() throws IOException {
        //com/spring/campusall/springsource/SpringSourceStudy.class
        //获取getResource 开始名称需要去掉/
        return this.classLoader.getResource(this.path);
    }

    @Override
    public File getFile() throws IOException {
        return null;
    }

    @Override
    public boolean isFile() {
        return false;
    }
}
